package cn.dida.dao;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.UUID;

import org.apache.commons.lang3.ArrayUtils;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.stereotype.Repository;

import cn.dida.model.Company;
import cn.dida.model.Pageable;

/**
 * 公司管理  数据交互类
 * @author weiwei
 */
@Repository
public class CompanyDao extends DaoBase {
	/**
	 * 根据公司id获取公司信息
	 * @param id 公司id
	 * @return Company 公司实体信息
	 * */
    public Company get(Integer id) {
        String sql = "select * from company where id = ?";
        try {
            Company Company = jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<Company>(Company.class), id);
            return Company;
        } catch (DataAccessException e) {
            return null;
        }
    }

    @Deprecated
    public Company get(String account, String pwd) {
        String sql = "select * from company where account = ? and pwd = ?";
        try {
            Company Company = jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<Company>(Company.class), account, pwd);
            return Company;
        } catch (DataAccessException e) {
            return null;
        }
    }

    /**
     * 公司列表
     * @param keyword 查询条件
     * @param state 状态（1：正常、2：停用）
     * @param page 分页游标
     * @return Pageable<Company> 公司分页列表
     * */
    public Pageable<Company> query(String keyword, Integer state, int page) {
        Object[] args = {};
        StringBuffer sqlQueryResultCount = new StringBuffer("select count(1) from company");
        StringBuffer sqlQueryResult = new StringBuffer("select * from company");

        StringBuffer sqlWhere = new StringBuffer(" where 1=1");
        if (state != null) {
            sqlWhere.append(" and state = ?");
            args = ArrayUtils.add(args, state);
        }
        if (keyword != null) {
            sqlWhere.append(" and name like ?");
            args = ArrayUtils.add(args, "%" + keyword + "%");
        }

        // 查询记录总数
        Integer resultTotal = jdbcTemplate.queryForObject(sqlQueryResultCount.append(sqlWhere).toString(), Integer.class, args);
        // 查询记录
        sqlQueryResult.append(sqlWhere).append("  order by create_time desc limit ?,?");
        args = ArrayUtils.add(args, (page - 1) * getPageSize());
        args = ArrayUtils.add(args, getPageSize());
        List<Company> result = jdbcTemplate.query(sqlQueryResult.toString(), args, new BeanPropertyRowMapper<>(Company.class));
        return new Pageable<>(page, getPageSize(), resultTotal, result);
    }

    /**
     * 公司列表
     * @param state 状态（1：正常、2：停用）
     * @return List<Company> 公司列表
     * */
    public List<Company> query(Integer state) {
        String sql = "select * from company where state = ?";
        List<Company> result = jdbcTemplate.query(sql.toString(), new BeanPropertyRowMapper<>(Company.class), state);
        return result;
    }

    /**
     * 新增公司
     * @param company 公司实体信息
     * @return int 受影响的记录行数
     * */
    public int insert(Company company) {
        String sql = "insert into company(name, preference, state, create_time, uuid) values(:name, :preference, :state, :create_time, :uuid)";
        HashMap<String, Object> paramMap = new HashMap<>();
        paramMap.put("name", company.getName());
        paramMap.put("preference", company.getPreference());
        paramMap.put("state", company.getState());
        paramMap.put("create_time", new Date());
        paramMap.put("uuid", UUID.randomUUID().toString());
        return namedParameterJdbcTemplate.update(sql, paramMap);
    }

    /**
     * 更新公司
     * @param company 公司实体信息
     * @return int 受影响的记录行数
     * */
    public int update(Company company) {
        String sql = "update company set name=:name, state=:state, preference=:preference where id = :id";
        HashMap<String, Object> paramMap = new HashMap<>();
        paramMap.put("id", company.getId());
        paramMap.put("preference", company.getPreference());
        paramMap.put("name", company.getName());
        paramMap.put("state", company.getState());
        return namedParameterJdbcTemplate.update(sql, paramMap);
    }

    /**
     * 删除公司
     * @param id 公司id
     * @return int 受影响的记录行数
     * */
    public int delete(Integer id) {
        String sql = "delete from company where id = ?";
        return jdbcTemplate.update(sql, id);
    }

}
